package pers.cj.pdos.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.GoodsSn;
import pers.cj.pdos.service.IGoodsService;
import pers.cj.pdos.service.IGoodsSnService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.PdosUtils;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.GoodsSnVo;
import pers.cj.pdos.vo.UserVo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品启用sn后:入库时需要与商品1对1入库。出库时也需要一对一出库 前端控制器
 *
 * sn号是同一商品不能重复，另外在新增商品和修改商品时前端要传入不重复的sn号即可。
 *
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/house")
public class GoodsSnController extends BaseController {
    @Autowired
    IGoodsSnService goodsSnService;
    @Autowired
    IGoodsService goodsService;

    @RequiresPermissions("goodsSn:query")
    @RequestMapping("/goodsSn")
    public String goodsSn(){
        return "/goodsSn/goodsSn";
    }

    @RequiresPermissions("goodsSn:query")
    @RequestMapping("/goodsSnDetail")
    public String goodsSnDetail(Model model, String id, String goodsId){
        model.addAttribute("id",id);
        model.addAttribute("goodsId",goodsId);
        return "/goodsSn/goodsSnDetail";
    }

    @ResponseBody
    @RequiresPermissions("goodsSn:query")
    @RequestMapping("/goodsSn/getList")
    public ResponseList getList(int page, int limit, String search, String storeId, String houseId, String inDate1,String inDate2, String outDate1,String outDate2,
                                String sn,String status, String no, String name){
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();

        List<GoodsSnVo> list = goodsSnService.listVoPage(company.getId(), page, limit,
                Utils.genLikeData(search),
                PdosUtils.getViewOtherStoreId(userVo, storeId),
                houseId,
                Utils.genDate(inDate1),
                Utils.genDate(inDate2),
                Utils.genDate(outDate1),
                Utils.genDate(outDate2),
                Utils.genLikeData(sn),
                Utils.genInteger(status),
                Utils.genLikeData(no),
                Utils.genLikeData(name));

        long count = goodsSnService.count(company.getId(),
                Utils.genLikeData(search),
                PdosUtils.getViewOtherStoreId(userVo, storeId),
                houseId,
                Utils.genDate(inDate1),
                Utils.genDate(inDate2),
                Utils.genDate(outDate1),
                Utils.genDate(outDate2),
                Utils.genLikeData(sn),
                Utils.genInteger(status),
                Utils.genLikeData(no),
                Utils.genLikeData(name));

        return ResponseList.success(count,list);
    }

    @ResponseBody
    @RequestMapping("/goodsSn/listOut")
    public ResponseList listOut(String page, String limit,String goodsId, String houseId, String search){
        if (Utils.isEmpty(goodsId) || Utils.isEmpty(houseId)) {
            return ResponseList.fail("商品id或仓库id为空");
        }
        List<GoodsSn> goodsSnList = goodsSnService.listOut(Utils.genInteger(page), Utils.genInteger(limit),goodsId, houseId, Utils.genLikeData(search));
        Long count = goodsSnService.countOut(goodsId,houseId,Utils.genLikeData(search));
        return ResponseList.success(count, goodsSnList);
    }

    @ResponseBody
    @RequestMapping("/goodsSn/listOutByGoodsTransferId")
    public ResponseList listOutByGoodsTransferId(String goodsTransferId, String goodsId){
        if (Utils.isEmpty(goodsTransferId) || Utils.isEmpty(goodsTransferId)) {
            return ResponseList.fail("调拨单id不能为空");
        }
        List<GoodsSn> goodsSnList = goodsSnService.listOutByGoodsTransferId(goodsTransferId,goodsId);
        return ResponseList.success(goodsSnList.size(), goodsSnList);
    }

    @ResponseBody
    @RequestMapping("/goodsSn/listOutByBorrowInId")
    public ResponseList listOutByBorrowInId(String borrowInId, String goodsId){
        if (Utils.isEmpty(borrowInId) || Utils.isEmpty(borrowInId)) {
            return ResponseList.fail("借入单id不能为空");
        }
        List<GoodsSn> goodsSnList = goodsSnService.listOutByBorrowInId(borrowInId,goodsId);
        return ResponseList.success(goodsSnList.size(), goodsSnList);
    }

    @ResponseBody
    @RequestMapping("/goodsSn/listOutByBorrowOutId")
    public ResponseList listOutByBorrowOutId(String borrowOutId, String goodsId){
        if (Utils.isEmpty(borrowOutId) || Utils.isEmpty(borrowOutId)) {
            return ResponseList.fail("借入单id不能为空");
        }
        List<GoodsSn> goodsSnList = goodsSnService.listOutByBorrowOutId(borrowOutId,goodsId);
        return ResponseList.success(goodsSnList.size(), goodsSnList);
    }

    @ResponseBody
    @RequestMapping("/goodsSn/checkRepeat")
    public ResponseEntity checkRepeat(String goodsId, @RequestBody List<String> list){
        if (Utils.isEmpty(list)){
            return ResponseEntity.success();
        }
        boolean repeat = goodsSnService.checkRepeat(goodsId, list);
        if (repeat){
            return ResponseEntity.fail("有重复序列号，请重新录入");
        }
        return ResponseEntity.success();
    }

    /**
     * 检查list中的sn是否在houseId以外的仓库中有在库的sn. 若存在怎返回
     * @param goodsId
     * @param houseId
     * @param list
     * @return
     */
    @ResponseBody
    @RequestMapping("/goodsSn/checkSnStatusOutHouseId")
    public ResponseData checkSnStatusOutHouseId(String goodsId, String houseId, @RequestBody List<String> list){
        if (Utils.isEmpty(list)){
            return ResponseData.fail("参数序列号不能为空");
        }
        if (Utils.isEmpty(goodsId) || Utils.isEmpty(houseId)){
            return ResponseData.fail("参数goodsId、houseId不能为空");
        }

        List<GoodsSn> goodsSnList = goodsSnService.checkSnStatusOutHouseId(goodsId, houseId, list);
        return ResponseData.success(goodsSnList);
    }


    @ResponseBody
    @RequiresPermissions("goodsSn:query")
    @RequestMapping("/goodsSn/getVoById")
    public ResponseData getVoById(String id){
        if (Utils.isEmpty(id)) {
            return ResponseData.fail("参数不能为空");
        }

        GoodsSnVo goodsSnVo = goodsSnService.getVoById(id);
        if (goodsSnVo == null){
            return ResponseData.fail("未找到对应信息");
        }

        return ResponseData.success(goodsSnVo);
    }
}
